//
//  AlbumView.m
//  UI6_轮播
//
//  Created by dllo on 16/3/16.
//  Copyright © 2016年 www.lanou.com 蓝欧科技. All rights reserved.
//

#import "AlbumView.h"
#import "MyScrollView.h"



@interface AlbumView () <UIScrollViewDelegate>
@property (nonatomic, retain) MyScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *page;

// i轮播时  控制图片
@property (nonatomic, assign) NSInteger i ;

@property (nonatomic, retain) NSMutableArray *scrollImageArr;
@property (nonatomic, retain) NSTimer *timer;
@end

@implementation AlbumView



- (instancetype)initWithFrame:(CGRect)frame
  imageArr:(NSArray<UIImage *> *)imageArr
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView:frame imageArr:imageArr];
    }
    return self;
}
- (void)createView:(CGRect)fram imageArr:(NSArray *)imageArr
{
    self.scrollView = [[MyScrollView alloc]initWithFrame:fram imageArr:imageArr delegate:self];/** 小scrollView代理人 */
    // 设置代理人(大的ScrollVeiw执行拖拽..功能)
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];
    
    // self.scrollView添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(passValue:)];
    [self.scrollView addGestureRecognizer:tap];
    // 遍历数组
    self.scrollImageArr = [NSMutableArray array];
    for (int i = 1; i < 5; i++) {
        // 去除数组中的第一张 和最后一张 添加到self.scrollImageArr
        [self.scrollImageArr addObject:imageArr[i]];
    }
    
    
    
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, fram.size.height - 130, fram.size.width, 40)];
    [self addSubview:self.page];
    self.page.currentPageIndicatorTintColor = [UIColor redColor];
    self.page.pageIndicatorTintColor = [UIColor blackColor];
    // 控制page个数
    self.page.numberOfPages = imageArr.count - 2;
    //
    [self.page addTarget:self action:@selector(back:) forControlEvents:UIControlEventValueChanged];
    // 记录图片的个数 
    self.i = imageArr.count;
   
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(roll) userInfo:nil repeats:YES];
    
}
- (void)roll
{
    
    // 从头滚
    if (self.scrollView.contentOffset.x == self.scrollView.frame.size.width * (self.i - 2)) {
        self.scrollView.contentOffset = CGPointZero;
    }
    // 翻页
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + self.scrollView.frame.size.width , 0) animated:YES];
   // page随图片变化
    self.page.currentPage = self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
    // 当翻到最后一页 页码置为0
    if (self.scrollView.contentOffset.x == self.scrollView.frame.size.width * (self.i - 2) ) {
        self.page.currentPage = 0;
    }
    
}
// 拖拽 page随页面变化
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
#pragma mark - 不轮播时  缩放好用  轮播时缩放 有bug
     // (小的scrollView进行了缩放)判断滑动的ScrollView是大ScrollView的时候
    if (scrollView == self.scrollView) {
        for (UIScrollView *smallScrollView in self.scrollView.subviews) {
            if ([smallScrollView.class isSubclassOfClass:[UIScrollView class]]) {
                smallScrollView.zoomScale = 1;
            }
        }
    }
    // 右拖
    if (scrollView.contentOffset.x == self.scrollView.bounds.size.width * (self.i - 1)) {
        scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    }
    // 左拖
    if (scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * (self.i - 2), 0);
    }
    NSLog(@"%f", scrollView.contentOffset.x);
    // page变化
    self.page.currentPage = (scrollView.contentOffset.x - self.scrollView.bounds.size.width)/ self.scrollView.bounds.size.width;
    
    // 重新开始轮播
  self.timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(roll) userInfo:nil repeats:YES];
    
}
// 点击页码  page跟随变化
- (void)back:(UIPageControl *)back
{
    
    [self.scrollView setContentOffset:CGPointMake((back.currentPage + 1)* self.bounds.size.width , 0) animated:YES];
}
// 手势方法
- (void)passValue:(UITapGestureRecognizer *)passValue
{
    
    [self.delegate passCurrentPage:self.scrollImageArr[self.page.currentPage]];
    
}
// 将要开始推拽的时候 停止轮播
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.timer invalidate];
    self.timer = nil;
}

// 缩放视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    if (scrollView != self.scrollView) {
        return [scrollView.subviews firstObject];
    }else
    {
        return nil;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
